<?php

namespace app\api\model;

use think\Model;

class Base extends Model {
    public $autoWriteTimestamp = true;
    protected  $mid;

    /**
     * 构造函数-定义mid
     * @date 2018-09-30 16:15
     * @param $data
     */
    public function __construct($data = []) {

        parent::__construct($data);
        if (isset($data['uid'])) {
            $mid = $data['uid'];
        } else {
            if (defined('WID')) {
                $mid = WID;
            } elseif (defined('UID')) {
                $mid = UID;
            }else{
               $mid =1;
            }
        }
        $this->mid=$mid;
    }
    //日志关联产品表
    public function warehouse(){
        return $this->hasOne("StockWarehouse","id","warehouse_id")->bind(['warehouse_name'=>'name','category_id']);
    }
    /**
     * 查找一条数据
     * @param $where  查询条件
     * @param string $field 查询字段
     * @return array|false|\PDOStatement|string|Model
     */
    public function findOne($where = [], $field = '*',$order = "",$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',1];
        }
         if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $array = explode(',', $field);
        if ($field != '*') {
            if (count($array) <= 1) {
                return $this->where($where)->value($field);
            } else {
                return $this->field($field)->where($where)->order($order)->find();
            }
        } else {
            return $this->where($where)->order($order)->find();
        }
    }
    /**
     * 查找一条数据
     * @param $where  查询条件
     * @param string $field 查询字段
     * @return array|false|\PDOStatement|string|Model
     */
    public function findOneWith($with,$where = [], $field = '*',$order = "",$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',1];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $array = explode(',', $field);
        if ($field != '*') {
            if (count($array) <= 1) {
                return $this->where($where)->value($field);
            } else {
                return $this->with($with)->field($field)->where($where)->order($order)->find();
            }
        } else {
            return $this->with($with)->where($where)->order($order)->find();
        }
    }
    /**
     * 获取列表信息（无分页）
     * @param $where   查询条件
     * @param string $field 查询字段
     * @param string $order 排序方式
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function selectList($where, $field = '*', $order = '',$is_uid=0) {
         if (!isset($where['uid'])&&$is_uid==1) {
             $where[]=['uid','eq',1];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        return $this->field($field)->where($where)->order($order)->select();
    }
    /**
     * 获取列表信息（无分页）
     * @param $where   查询条件
     * @param string $field 查询字段
     * @param string $order 排序方式
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function selectListWith($with,$where, $field = '*', $order = '',$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',1];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        return $this->with($with)->field($field)->where($where)->order($order)->select();
    }
    /**
     * 获取列表单列信息（无分页）
     * @param $where  查询条件
     * @param string $field 查询字段
     * @param string $order 排序方式
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function selectListByColumn($where, $field = '', $order = '' ,$is_uid=0) {
          if (!isset($where['uid'])&&$is_uid==1) {
              $where[]=['uid','eq',1];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        return $this->where($where)->order($order)->column($field);
    }

    /**
     * 修改/新增数据
     * @param $data
     * @return bool|mixed
     */
    public function dataUpdate($data,$is_uid=0) {
        if (empty($data)) {
            return false;
        } else {
            if (array_key_exists($this->getPk(), $data) && !empty($data[$this->getPk()])) { // 数据修改
                $result = $this->allowField(true)->edit($data,$is_uid);
            } else { // 数据插入
                $result = $this->allowField(true)->add($data,$is_uid);
            }
            return $result;
        }
    }

    /**
     * 新增数据
     * @param $data
     * @return bool|mixed
     */
    public function add(array $data = [],$is_uid=0) {
        if (!empty($data)) {
              if (!isset($data['uid'])&&$is_uid==1) {
                $data['uid'] = $this->mid;
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            $res = $this->allowField(true)->insertGetId($data);
            if ($res > 0) {
                return $res;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 修改数据
     * @param $data
     * @return bool|mixed
     */
    public function edit(array $data = [],$is_uid=0) {
        if (!empty($data)) {
              if (!isset($data['uid'])&&$is_uid==1) {
                $data['uid'] = $this->mid;
            }
            $data['update_time'] = time();
            $res = $this->allowField(true)->update($data);
            if ($res) {
                return $data[$this->getPk()];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 后台异步加载列表
     *
     * @param $where 筛选条件
     * @param $search 底部分页条件
     * @param $field  筛选字段
     * @param string $order 排序方式
     * @return $this
     */
    public function getMemberList($where, $search, $field = "*", $order = '', $per_page_number = 15,$is_uid=0) {
          if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        return $this->where($where)->field($field)->order($order)->paginate($per_page_number, false, [
                    'page' => array_key_exists("page", $search) ? $search['page'] : 1,
        ]);
    }

    /**
     * 前台单表异步查询(limit 方式)
     * @param $where  筛选条件
     * @param $field  筛选字段
     * @param $start  获取数据开始的条数（第多少条）
     * @param $limit  获取条数
     * @param string $order 排序条件
     * @return array
     */
    public function selectListbyLimit($where, $field = '*', $start = 0, $limit = 15, $order = '',$is_uid=0) {
          if (!isset($where['uid'])&&$is_uid==1) {
              $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $dataList = $this->field($field)->where($where)->limit($start, $limit)->order($order)->select();
        return $dataList;
    }

    /**
     * 前台单表异步查询（带分页）
     * @param $where 查询条件
     * @param $field 查询字段
     * @param string $order 排序条件
     * @param $per_page_number  默认页数
     * @return array
     */
    public function wechatList($where, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
         if (!isset($where['uid'])&&$is_uid==1) {
             $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if($data['page']==0){
            $dataList = $this->field($field)->where($where)->order($order)->select();
            foreach ($dataList as $key=>&$item){
                if(isset($item['cover_img'])){
                    $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                }
            }
            $dataList = $dataList->toArray();
            return $dataList;
        }else{
            if (!isset($data['submit_time'])) {
                $dataList = $this->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                    'page' => $data['page']])->each(function ($item ,$key){
//                    //如果存在图片将图片转成绝对路径
                    if(isset($item['cover_img'])){
                        $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                    }
                    return $item;
                });
            } else {
                $dataList = $this::withTrashed()
                    ->field($field)
                    ->where($where)
                    ->where(function ($query) use ($data) {
                        $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                            $query_1->where('delete_time', 'gt', $data['submit_time']);
                        });
                    })
                    ->order($order)
                    ->paginate($per_page_number, false, [
                        'page' => $data['page']]);
            }
            $dataList=$dataList->toArray();
            return $dataList['data'];
        }

    }
    /**
     * 前台单表异步查询（带分页）
     * @param $where 查询条件
     * @param $field 查询字段
     * @param string $order 排序条件
     * @param $per_page_number  默认页数
     * @return array
     */
    public function wechatListTotal($where, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if($data['page']==0){
            $dataList = $this->field($field)->where($where)->order($order)->select();
            foreach ($dataList as $key=>&$item){
                if(isset($item['cover_img'])){
                    $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                }
            }
            $dataList = $dataList->toArray();
            $returnList = [
                'data'=>$dataList,
                'count'   =>count($dataList),
            ];
            return $returnList;
        }else{
            if (!isset($data['submit_time'])) {
                $dataList = $this->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                    'page' => $data['page']])->each(function ($item ,$key){
//                    //如果存在图片将图片转成绝对路径
                    if(isset($item['cover_img'])){
                        $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                    }
                    return $item;
                });
            } else {
                $dataList = $this::withTrashed()
                    ->field($field)
                    ->where($where)
                    ->where(function ($query) use ($data) {
                        $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                            $query_1->where('delete_time', 'gt', $data['submit_time']);
                        });
                    })
                    ->order($order)
                    ->paginate($per_page_number, false, [
                        'page' => $data['page']]);
            }
            $dataList=$dataList->toArray();
            $returnList = [
                'data'=>$dataList['data'],
                'count'=>$dataList['total']
            ];
            return $returnList;
        }
    }
    /**
     * 前台单表异步查询（带分页）
     * @param $where 查询条件
     * @param $field 查询字段
     * @param string $order 排序条件
     * @param $per_page_number  默认页数
     * @return array
     */
    public function wechatListTotalWith($with,$where, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if($data['page']==0){
            $dataList = $this->with($with)->field($field)->where($where)->order($order)->select();
            foreach ($dataList as $key=>&$item){
                if(isset($item['cover_img'])){
                    $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                }
                if(isset($item['logo'])){
                    $item['logo'] = imgUrlToAbsolute($item['logo']);
                }
            }
            $dataList = $dataList->toArray();
            $returnList = [
                'data'=>$dataList,
                'count'   =>count($dataList),
            ];
            return $returnList;
        }else{
            if (!isset($data['submit_time'])) {
                $dataList = $this->with($with)->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                    'page' => $data['page']])->each(function ($item ,$key){
//                    //如果存在图片将图片转成绝对路径
                    if(isset($item['cover_img'])){
                        $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                    }
                    if(isset($item['logo'])){
                        $item['logo'] = imgUrlToAbsolute($item['logo']);
                    }
                    return $item;
                });
            } else {
                $dataList = $this::withTrashed()
                    ->with($with)
                    ->field($field)
                    ->where($where)
                    ->where(function ($query) use ($data) {
                        $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                            $query_1->where('delete_time', 'gt', $data['submit_time']);
                        });
                    })
                    ->order($order)
                    ->paginate($per_page_number, false, [
                        'page' => $data['page']]);
            }
            $dataList=$dataList->toArray();
            $returnList = [
                'data'=>$dataList['data'],
                'count'=>$dataList['total']
            ];
            return $returnList;
        }
    }
    public function wechatListTotalHasWhere($with,$where,$where2, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        $dataList = $this->hasWhere($with,$where2)->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
            'page' => $data['page']])->each(function ($item,$key){
            if(isset($item['user'])){

            }
            if(isset($item['detail'])){

            }
            if(isset($item['cover_img'])){
                $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
            }
            return $item;

        });

        $dataList=$dataList->toArray();
        $returnData = [
          'data' =>$dataList['data'],
          'count'=>$dataList['total']
        ];
        return $returnData;
    }
    /**
     * 前台单表异步查询（带分页）
     * @param $where 查询条件
     * @param $field 查询字段
     * @param string $order 排序条件
     * @param $per_page_number  默认页数
     * @return array
     */
    public function wechatListHasOne($with,$where, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if (!isset($data['submit_time'])) {
            $dataList = $this->with($with)->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                'page' => $data['page']])->each(function ($item,$key){
                    $item['create_time'] = date("Y-m-d H:i",$item['create_time']);
                    return $item;
            });
        } else {
            $dataList = $this::withTrashed()
                ->with($with)
                ->field($field)
                ->where($where)
                ->where(function ($query) use ($data) {
                    $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                        $query_1->where('delete_time', 'gt', $data['submit_time']);
                    });
                })
                ->order($order)
                ->paginate($per_page_number, false, [
                    'page' => $data['page']]);
        }
        $dataList=$dataList->toArray();
        return $dataList['data'];
    }
    public function wechatListHasWhere($with,$where,$where2, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if (!isset($data['submit_time'])) {
            $dataList = $this->hasWhere($with,$where2)->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                'page' => $data['page']])->each(function ($item,$key){
                    if(isset($item['user'])){

                    }
                if(isset($item['cover_img'])){
                    $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                }
                return $item;

            });
        } else {
            $dataList = $this::withTrashed()
                ->with($with)
                ->field($field)
                ->where($where)
                ->where(function ($query) use ($data) {
                    $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                        $query_1->where('delete_time', 'gt', $data['submit_time']);
                    });
                })
                ->order($order)
                ->paginate($per_page_number, false, [
                    'page' => $data['page']]);
        }
        $dataList=$dataList->toArray();
        return $dataList['data'];
    }
    /**
     * 获取无限级数据 2018-07-25
     * @param $where 条件
     * @param string $order 排序
     * @param int $limit_level 查询几级结束
     * @param string $relation_id 关联商家ID字段名
     * @param string $keyword 返回数组中当期级别的名称
     * @return array
     */
    public function getInfinite($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level') {
        return $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, 0, []);
    }

    /**
     * 无限级数据查询方法 2018-07-25
     * @param $where
     * @param string $order
     * @param int $limit_level
     * @param string $relation_id
     * @param string $keyword
     * @param int $now_level
     * @param array $return_data
     * @return array
     */
    public function getInfiniteData($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level', $now_level = 0, $return_data = []) {
        if (!empty($limit_level)) {
            if ($limit_level <= $now_level) {
                return $return_data;
            }
        }
        if ($now_level == 0) {
            $where[$relation_id] = ['elt', 0];
        }
        if (empty($order)) {
            $order = $this->getPk() . ' desc';
        }
        $array = $this->where($where)->order($order)->select();
        if (empty($array)) {
            return $return_data;
        }

        foreach ($array as $key => $value) {
            $where[$relation_id] = $value[$this->getPk()];
            $value[$keyword] = $now_level;
            $return_data[] = $value;
            $return_data = $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, $now_level + 1, $return_data);
        }
        return $return_data;
    }

    /**
     * 获取无限级数据
     * @param $where 条件
     * @param string $order 排序
     * @param int $limit_level 查询几级结束
     * @param string $name 名称字段名
     * @param string $relation_id 关联商家ID字段名
     * @param string $keyword 返回数组中当期级别的名称
     * @return array
     */
    public function getInfiniteName($where, $order = '', $limit_level = 0, $name = 'name', $relation_id = 'pid', $keyword = 'level') {
        return $this->getInfiniteNameData($where, $order, $limit_level, $name, $relation_id, $keyword, 0, []);
    }

    /**
     * 无限级数据查询方法
     * @param $where
     * @param string $order
     * @param int $limit_level
     * @param string $name 名称字段名
     * @param string $relation_id
     * @param string $keyword
     * @param int $now_level
     * @param array $return_data
     * @return array
     */
    public function getInfiniteNameData($where, $order = '', $limit_level = 0, $name = 'name', $relation_id = 'pid', $keyword = 'level', $now_level = 0, $return_data = []) {
        if (!empty($limit_level)) {
            if ($limit_level <= $now_level) {
                return $return_data;
            }
        }
        if ($now_level == 0) {
            $where[$relation_id] = ['elt', 0];
        }
        if (empty($order)) {
            $order = $this->getPk() . ' desc';
        }
        $array = $this->where($where)->order($order)->select();
        if (empty($array)) {
            return $return_data;
        }

        foreach ($array as $key => $value) {
            if ($now_level != 0) {
                $value[$name] = $this->where($this->getPk(), $value[$relation_id])->value($name) . '-' . $value[$name];
            }
            $where[$relation_id] = $value[$this->getPk()];
            $value[$keyword] = $now_level;
            $return_data[] = $value;
            $return_data = $this->getInfiniteNameData($where, $order, $limit_level, $name, $relation_id, $keyword, $now_level + 1, $return_data);
        }
        return $return_data;
    }

    /**
     *  修改数据
     * @param string $field 修改字段名
     * @param string $field_value 修改值
     * @param string $primary_key 主键值
     * @param array $where 更新条件
     * @return bool
     */
    public function fieldUpdate($field = '', $field_value = '', $primary_key = '', $where =[]) {
        if (!empty($where)) {
            if($field=='is_publish'){
              $res = $this->isUpdate(true)->save([$field => $field_value,'publish_time'=>time()], $where);     
            }else{
              $res = $this->isUpdate(true)->save([$field => $field_value], $where);   
            }
        } else {
            if($field=="is_publish"){
              $res = $this->isUpdate(true)->save([$field => $field_value,'publish_time'=>time()], [[$this->getPk(),'in',$primary_key]]);
            }else{
             $res = $this->isUpdate(true)->save([$field => $field_value], [[$this->getPk(),'in',$primary_key]]);
            } 
        }
        if ($res === false) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 修改数据，数组中必须存在键值,否则为新增
     * @param array $where
     * @return array|false
     * @throws \Exception
     */
    public function fieldUpdateAll($where = array()) {
        return $this->saveAll($where);
    }

    /**
     * 查询符合条件的个数
     * @param $where 查询条件
     * @return int|string
     */
    public function getCount($where) {
        return $this->where($where)->count($this->getPk());
    }
    public function getCountHasWhere($with,$where,$where2) {
        return $this->hasWhere($with,$where2)->where($where)->count();
    }

    /**
     * 关联用户表
     * @date 2018-09-30
     * @return $this
     */
    public function withVip() {
        return $this->belongsTo('Vip', 'openid', 'openid');
    }


    /**
     * 生成订单编号 - 不同的表订单编号带不同的大写字母以区分
     * @author aurora
     * @date 2018-11-09
     * @param string $char 订单编号所带的大写字母
     * @param int $length 订单编号后字符串长度
     * @return int|string
     */
    public function createOrderNumber($char = "", $length = 4) {
        $count = 0;
        do {
            $order_number = $char . date('YmdHis');
            for ($i = 0; $i < $length; $i++) {
                $order_number .= rand(0, 9);
            }
            $count = $this->getCount(['order_number' => $order_number]);   //保证生成的订单编号不会重复
        } while ($count>0);

        return $order_number;
    }
    public function createOutTradeNo($char = "", $length = 4) {
        $count = 0;
        do {
            $order_number = $char . date('YmdHis');
            for ($i = 0; $i < $length; $i++) {
                $order_number .= rand(0, 9);
            }
            $count = $this->getCount(['out_trade_no' => $order_number]);   //保证生成的订单编号不会重复
        } while ($count>0);

        return $order_number;
    }
    public function SoftDelete($where){
        return $this->where($where)->setField("delete_time",time());
    }
    /**
     * 前台单表异步查询（带分页）
     * @param $where 查询条件
     * @param $field 查询字段
     * @param string $order 排序条件
     * @param $per_page_number  默认页数
     * @return array
     */
    public function wechatListWith($with,$where, $field = '*', $order = '', $per_page_number = 15,$is_uid=0) {
        if (!isset($where['uid'])&&$is_uid==1) {
            $where[]=['uid','eq',$this->mid];
        }
        if (empty($order)) {
            if ($this->getPk() != null) {
                $order = $this->getPk() . ' desc';
            } else {
                $order = 'id desc';
            }
        }
        $data = array();
        if (isset($where['page']) && $where['page'] != "") {
            $data['page'] = $where['page'];
            unset($where['page']);
        } else {
            $data['page'] = 1;
        }
        if (isset($where['submit_time']) && $where['submit_time'] != "") {
            $data['submit_time'] = $where['submit_time'];
            unset($where['submit_time']);
        }
        if (!isset($data['submit_time'])) {
            $dataList = $this->with($with)->field($field)->where($where)->order($order)->paginate($per_page_number, false, [
                'page' => $data['page']])->each(function ($item ,$key){
                //如果存在图片将图片转成绝对路径
                if(isset($item['cover_img'])){
                    $item['cover_img'] = imgUrlToAbsolute($item['cover_img']);
                }
                if(isset($item['photo'])){
                    $item['photo'] = imgUrlToAbsolute($item['photo']);
                }
                return $item;
            });
        } else {
            $dataList = $this::withTrashed()
                ->field($field)
                ->where($where)
                ->where(function ($query) use ($data) {
                    $query->where('delete_time', 'null')->whereor(function ($query_1) use ($data) {
                        $query_1->where('delete_time', 'gt', $data['submit_time']);
                    });
                })
                ->order($order)
                ->paginate($per_page_number, false, [
                    'page' => $data['page']]);
        }
        $dataList=$dataList->toArray();
        return $dataList['data'];
    }
}
